package com.qf.course.core.util;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * description:
 * author: Ken
 * 公众号：Java架构栈
 */
public class PinyinUtils {

    public static String str2pinyin(String name){
        try {
            //设置声调的格式
            HanyuPinyinOutputFormat hanyuPinyinOutputFormat = new HanyuPinyinOutputFormat();
            hanyuPinyinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);//取消声调

            //将汉字转换成拼音
            String pinyin = PinyinHelper.toHanYuPinyinString(name, hanyuPinyinOutputFormat);
            return pinyin;
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
        }
        return null;
    }
}
